<?php
namespace app\type\controller;

use think\Controller;
use think\Db;
use app\type\model\TypeModel;
use app\food\model\FoodModel;

class Apis extends Controller
{
    public function addType($type)
    {
        $type['type_ctime'] = date('Y-m-d H:i:s', time());
        $TypeModel = new TypeModel();
        $res = $TypeModel->addType($type);
        return json($res);
    }

    public function delTypeById($id)
    {
        $TypeModel = new TypeModel();
        $res = $TypeModel->delType($id);
        return json($res);
    }

    public function upTypeById($type)
    {
        $type['type_utime'] = date('Y-m-d H:i:s', time());
        $TypeModel = new TypeModel();
        $res = $TypeModel->upType($type);
        return json($res);
    }

    public function getAllType($type)
    {
        $TypeModel = new TypeModel();
        $FoodModel = new FoodModel();
        $types = $TypeModel->getType($type);
        $foods = $FoodModel->getFood(null);
        foreach ($types as $key => $type) {
            $types[$key]['goods'] = [];
            foreach($foods as $key2 => $food){
                if($food['type_id'] == $type['type_id']){
                    array_push($types[$key]['goods'], $food['food_id']);
                }
            }
        }

        return json($types);
    }



    public function getType($type)
    {
        $TypeModel = new TypeModel();
        $types = $TypeModel->getType($type);
        return $types;
    }

    public function getTypeByApp($app)
    {
        $type['type_app'] = $app;
        return $this->getAllType($type);
    }

}
